package com.lintf.sharding.sphere.algorithm;


import org.apache.shardingsphere.sharding.api.sharding.standard.PreciseShardingValue;
import org.apache.shardingsphere.sharding.api.sharding.standard.RangeShardingValue;
import org.apache.shardingsphere.sharding.api.sharding.standard.StandardShardingAlgorithm;

import java.util.Collection;
import java.util.Properties;

/**
 * @author lin tingfeng
 * @system Mac
 * @data 2024/10/24 10:24
 * @package_name com.lintf.shardingSphere.algorithm
 * @project_name spring-ai
 * @desc
 */

public class CityShardingAlgorithm implements StandardShardingAlgorithm<String> {

    @Override
    public String doSharding(Collection<String> availableTargetNames, PreciseShardingValue<String> shardingValue) {
        String logicTableName = shardingValue.getLogicTableName();
        String value = shardingValue.getValue();
        String tableName = logicTableName + "_" + value;
        if (availableTargetNames.contains(tableName)) {
            return tableName;
        }
        throw new IllegalArgumentException("No suitable table found for sharding value: " + shardingValue);
    }

    @Override
    public Collection<String> doSharding(Collection<String> availableTargetNames, RangeShardingValue<String> shardingValue) {
        return availableTargetNames;
    }

    @Override
    public Properties getProps() {
        return null;
    }

    @Override
    public void init(Properties properties) {

    }
}